package com.example.teaching_server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Size;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;

/**
 * 教学资源实体类
 *
 * @author Teaching Server
 * @since 1.0.0
 */
@Getter
@Setter
@ToString
@EqualsAndHashCode(of = "id")
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "resources", indexes = {
        @Index(name = "idx_resource_type", columnList = "type"),
        @Index(name = "idx_resource_subject", columnList = "subject"),
        @Index(name = "idx_resource_uploader", columnList = "uploader_id")
})
public class Resource {

    /**
     * 资源ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 资源名称
     */
    @NotBlank(message = "资源名称不能为空")
    @Size(max = 100, message = "资源名称长度不能超过100个字符")
    @Column(nullable = false, length = 100)
    private String name;

    /**
     * 资源类型
     */
    @NotBlank(message = "资源类型不能为空")
    @Size(max = 20, message = "资源类型长度不能超过20个字符")
    @Column(nullable = false, length = 20)
    private String type;

    /**
     * 文件大小
     */
    @NotBlank(message = "文件大小不能为空")
    @Size(max = 20, message = "文件大小长度不能超过20个字符")
    @Column(nullable = false, length = 20)
    private String size;

    /**
     * 文件URL
     */
    @NotBlank(message = "文件URL不能为空")
    @Size(max = 255, message = "文件URL长度不能超过255个字符")
    @Column(name = "file_url", nullable = false, length = 255)
    private String fileUrl;

    /**
     * 所属学科
     */
    @Size(max = 50, message = "所属学科长度不能超过50个字符")
    @Column(length = 50)
    private String subject;

    /**
     * 上传者ID
     */
    @NotNull(message = "上传者ID不能为空")
    @Column(name = "uploader_id", nullable = false)
    private Integer uploaderId;

    /**
     * 文件类型
     */
    @Size(max = 50, message = "文件类型长度不能超过50个字符")
    @Column(name = "file_type")
    private String fileType;

    /**
     * 创建时间
     */
    @CreationTimestamp
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "created_at", nullable = false, updatable = false)
    private LocalDateTime createdAt;

    /**
     * 更新时间
     */
    @UpdateTimestamp
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    @Column(name = "updated_at", nullable = false)
    private LocalDateTime updatedAt;
}
